package com.allan.springboot_wms.service;


import com.allan.springboot_wms.domain.User;
import com.allan.springboot_wms.domain.UserExample;
import com.allan.springboot_wms.exception.BusinessException;
import com.allan.springboot_wms.exception.BusinessExceptionCode;
import com.allan.springboot_wms.mapper.UserMapper;
import com.allan.springboot_wms.req.UserQueryReq;
import com.allan.springboot_wms.req.UserSaveReq;
import com.allan.springboot_wms.resp.UserLoginResp;
import com.allan.springboot_wms.resp.UserResp;
import com.allan.springboot_wms.resp.PageResp;
import com.allan.springboot_wms.util.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

import java.util.List;

@Service
public class UserService {

    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);

    //导入雪花算法    Snowflake算法是Twitter发明的一种算法，用于在分布式、高并发环境中，生成64位自增ID。
//    @Resource
//    private SnowFlake snowFlake;
    @Resource
    private UserMapper userMapper;


    public PageResp<UserResp> list(UserQueryReq req) {
        //mybatis的逆向工程中会生成实例及实例对应的example，example用于添加条件，相当于where后面的部分
        //比如criteria.andXxxEqualTo(value)，添加Xxx字段等于value的条件
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getAdmin())) {  //判断是否为空 不为空有名字的话 按名字来查找
            criteria.andAdminLike(("%" + req.getAdmin() + "%"));
        }
        PageHelper.startPage(req.getPage(), req.getSize()); //就相当于在数据库中加了LIMIT限制
        //先转成user类然后再遍历得到UserResp
        List<User> userList = userMapper.selectByExample(userExample);
        //pageinfo放入得到总页数或总行数,并在日志中打印出
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());    //一般传给前端传总行数就可
        //列表复制
        List<UserResp> list = CopyUtil.copyList(userList, UserResp.class);
        //把返回的列表添加到PageResp
        PageResp<UserResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }


    public void delete(String admin){
        userMapper.deleteByPrimaryKey(admin);
    }

    /**
     * 保存
     */
    public void save(UserSaveReq req){
        User user = CopyUtil.copy(req, User.class);
        User userDB = selectByAdmin(req.getAdmin());
        if(ObjectUtils.isEmpty(userDB)){
            userMapper.insert(user);
        }else{
            //用户名已经存在
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
        }

    }
    //依据用户名选择
    public User selectByAdmin(String admin){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andAdminEqualTo(admin);
        List<User> userList = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }else{
            return userList.get(0);
        }
    }

    public void update(UserSaveReq req){
        User user = CopyUtil.copy(req, User.class);
        userMapper.updateByPrimaryKey(user);
    }

    /**
     * 登录
     */
    public UserLoginResp login(UserQueryReq req){
        User userDB = selectByAdmin(req.getAdmin());
        if(ObjectUtils.isEmpty(userDB)){
            //用户名不存在
            LOG.info("用户名不存在：{}", req.getAdmin());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userDB.getPwd().equals(req.getPwd())){
                //登陆成功
                UserLoginResp userLoginResp = CopyUtil.copy(userDB, UserLoginResp.class);
                return userLoginResp;
            } else{
                //密码不对
                LOG.info("密码不对，输入密码为：{}，数据库密码为：{}", req.getPwd(), userDB.getPwd());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
